const express = require('express'); //引入express
const router = express.Router();
const { Dept } = require('../../models/dept');
var session = require('express-session');
// const { Role } = require('../../models/roles');
	// res.status(200).json({
	// 	code: 200,
	// 	msg: '用户列表查询成功',
	// });
// 增加角色
router.post('/add', (req, res) => {
	console.log(req.body);
	const data=req.body
	new Menu(data).save().then(result => {
		res.json({
			data: result,
			msg: '添加成功'
		});
	});
});
// 查询
router.get('/search', async (req, res) => {
  const data = await Dept.find();
  console.log(data,"date");
  
	res.json({
		code:200,
		msg: '菜单查询成功',
		data: data,
	});
});

// 删除
router.post("/delete/:id",(req, res) => {
	Menu.findByIdAndDelete({_id:req.params.id}).then(profile=>{
		 console.log(profile);
		 res.json({
			data: profile,
			msg: '删除成功'
		});
	}).catch(err=>{
		res.json({
			data: err,
			msg: '删除失败'
		});
	})
})
// 修改
router.post("/edit/:id",(req, res) => {
	let menuItem={
			path:req.body.path,
			parent:req.body.parent,
			name:req.body.name,
			icon:req.body.icon,
			link:req.body.link
	}
	Menu.findByIdAndUpdate(
			{_id:req.params.id},
			{$set:menuItem},
			{new:true}
	).then(profile=>{
		res.json({
			code:200,
			msg: '菜单修改成功',
			data: profile,
		});
	}).catch(err=>res.json(err))
})


module.exports = router;
